﻿using System.Dynamic;

namespace Ls.Data
{
	public partial class DbContext
	{
        public ISelectBuilder<TEntity> Select<TEntity>(string sql)
        {
            return Select<TEntity>(sql, false);
        }

        public ISelectBuilder<TEntity> Select<TEntity>(string sql, bool  isReadOnly  )
		{
			return new SelectBuilder<TEntity>(CreateCommand(isReadOnly)).Select(sql);
		}

		public IInsertBuilder Insert(string tableName)
		{
			return new InsertBuilder(CreateCommand(), tableName);
		}

		public IInsertBuilder<T> Insert<T>(string tableName, T item)
		{
			return new InsertBuilder<T>(CreateCommand(), tableName, item);
		}

		public IInsertBuilderDynamic Insert(string tableName, ExpandoObject item)
		{
			return new InsertBuilderDynamic(CreateCommand(), tableName, item);
		}

		public IUpdateBuilder Update(string tableName)
		{
			return new UpdateBuilder(Data.FluentDataProvider, CreateCommand(), tableName);
		}

		public IUpdateBuilder<T> Update<T>(string tableName, T item)
		{
			return new UpdateBuilder<T>(Data.FluentDataProvider, CreateCommand(), tableName, item);
		}

		public IUpdateBuilderDynamic Update(string tableName, ExpandoObject item)
		{
			return new UpdateBuilderDynamic(Data.FluentDataProvider, CreateCommand(), tableName, item);
		}

		public IDeleteBuilder Delete(string tableName)
		{
			return new DeleteBuilder(CreateCommand(), tableName);
		}

		public IDeleteBuilder<T> Delete<T>(string tableName, T item)
		{
			return new DeleteBuilder<T>(CreateCommand(), tableName, item);
		}

		private void VerifyStoredProcedureSupport()
		{
			if (!Data.FluentDataProvider.SupportsStoredProcedures)
				throw new FluentDataException("The selected database does not support stored procedures.");
		}
        public IStoredProcedureBuilder StoredProcedure(string storedProcedureName, bool isReadOnly)
        {
            VerifyStoredProcedureSupport();
            return new StoredProcedureBuilder(CreateCommand(isReadOnly), storedProcedureName);
        }
        public IStoredProcedureBuilder StoredProcedure(string storedProcedureName)
        {
            return StoredProcedure(storedProcedureName, false);
        }

		public IStoredProcedureBuilder<T> StoredProcedure<T>(string storedProcedureName, T item, bool isReadOnly)
		{
			VerifyStoredProcedureSupport();
			return new StoredProcedureBuilder<T>(CreateCommand(isReadOnly), storedProcedureName, item);
		}
         
        public IStoredProcedureBuilder<T> StoredProcedure<T>(string storedProcedureName, T item)
        {
            return StoredProcedure(storedProcedureName, item, false);
        }

        public IStoredProcedureBuilderDynamic StoredProcedure(string storedProcedureName, ExpandoObject item, bool isReadOnly)
        {
            VerifyStoredProcedureSupport();
            return new StoredProcedureBuilderDynamic(CreateCommand(isReadOnly), storedProcedureName, item);
        }

        public IStoredProcedureBuilderDynamic StoredProcedure(string storedProcedureName, ExpandoObject item)
        {
            return StoredProcedure(storedProcedureName, item, false);
        }
    }
}
